// pages/register/register.js
let app=getApp();
let testAddr = app.globalData.testAddr;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    phonePlaceholder:"请填写手机号",
    code:"",
    checkCode:"",
    codePlaceholder:"请填写短信验证码",
    isAllow:false
  },

  //获取验证码
  sendCode:function(){
    let that=this;
    if (!that.data.phone){
      wx.showToast({
        title: '手机号不能为空',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (!(/^1[34578]\d{9}$/.test(that.data.phone))){
      wx.showToast({
        title: '请输入正确手机号',
        icon: "none",
        duration: 2000
      })
      return;
    }
    app.util.request({
      url: testAddr + "sendNews/sendReg?phone=" + that.data.phone,
      method: "POST",
      data: {
        
      },
      success: function (res) {
        let code = res.data.value
        console.log(res);
        if(code){
          wx.showToast({
            title: '发送成功',
            duration: 2000
          })
          that.setData({
            checkCode: code
          });
        }else{
          wx.showToast({
            title: '发送失败',
            icon:"none",
            duration: 2000
          })
        }
        
      },
      fail: function (res) {

      },
      complete: function (res) {

      },
    });
  },
  //点击同意
  clickAllow:function(){
    this.setData({
      isAllow:true
    });
  },
  bindPhone:function(){
    let that = this;
    if (!that.data.phone) {
      wx.showToast({
        title: '手机号不能为空',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (!(/^1[34578]\d{9}$/.test(that.data.phone))) {
      wx.showToast({
        title: '请输入正确手机号',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (!that.data.code){
      wx.showToast({
        title: '请输入验证码',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (that.data.code!=that.data.checkCode) {
      wx.showToast({
        title: '验证码错误',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (!that.data.isAllow) {
      wx.showToast({
        title: '请选择同意协议',
        icon: "none",
        duration: 2000
      })
      return;
    }
    let id = app.globalData.user.guestid;
    app.util.request({
      url: testAddr + "Guest/updateInfo?phone=" + that.data.phone + "&guestID=" + 1169,
      method: "POST",
      data: {
        
      },
      success: function (res) {
        wx.showToast({
          title: '绑定成功',
          duration: 2000
        })   
      },
      fail: function (res) {

      },
      complete: function (res) {

      },
    });
  },
  //监听验证码和手机输入
  getCode:function(e){
    this.setData({
      code: e.detail.value
    });
  },
  getPhone:function(e){
    this.setData({
      phone: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})